package com.administration.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.administration.dao.AdministrationDao;
import com.administration.dao.impl.AdministrationDaoImpl;
import com.administration.entity.Access;
import com.administration.entity.Category;
import com.administration.entity.Groups;
import com.administration.entity.GroupPrivilege;
import com.administration.entity.Privilege;
import com.administration.entity.User;
import com.administration.entity.resultsetmapping.PrivilegeMapping;
import com.administration.service.AdministrationService;
import com.common.Constant;
import com.common.Utility;
import com.entity.RegisteredUser;

public class AdministrationServiceImpl implements AdministrationService,
		Serializable {
	private static final long serialVersionUID = 1L;
	AdministrationDao administrationDao = new AdministrationDaoImpl();
	RegisteredUser registeredUser;
	HttpSession session;

	public AdministrationServiceImpl() {
		session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);

		registeredUser = (RegisteredUser) session
				.getAttribute("registeredUser");
		if (registeredUser == null)
			registeredUser = new RegisteredUser();
	}

	// ----------------------------------------------------------//
	// -------------------------Category-------------------------//
	// ----------------------------------------------------------//
	public Category getCategoryById(Long categoryid) throws Exception {
		return administrationDao.getCategoryById(categoryid);

	}

	public List<Category> getAllCategories() throws Exception {
		return administrationDao.getAllCategories();
	}

	// ----------------------------------------------------------//
	// -------------------------Group----------------------------//
	// ----------------------------------------------------------//

	public Long addGroup(Groups group) throws Exception {
		return administrationDao.addGroup(group);
	}

	public void deleteGroup(Groups group) throws Exception {
		administrationDao.deleteGroup(group);
	}

	public Groups getGroupById(Long id) throws Exception {
		return administrationDao.getGroupById(id);
	}

	public void updateGroup(Groups group) throws Exception {
		administrationDao.updateGroup(group);
	}

	public List<Groups> getAllGroups() throws Exception {
		return administrationDao.getAllGroups();
	}

	// ----------------------------------------------------------//
	// -------------------------Access----------------------------//
	// ----------------------------------------------------------//

	public Long addAccess(Access access) throws Exception {
		return administrationDao.addAccess(access);
	}

	public void updateAccess(Access access) throws Exception {
		administrationDao.updateAccess(access);
	}

	public void delateAccess(Access access) throws Exception {
		administrationDao.delateAccess(access);
	}

	public List<Access> getAllAccess() throws Exception {
		return administrationDao.getAllAccess();
	}

	// ----------------------------------------------------------------//

	public Long addPrivilege(Privilege privilage) throws Exception {
		return administrationDao.addPrivilege(privilage);
	}

	public void updatePrivilege(Privilege privilage) throws Exception {
		administrationDao.updatePrivilege(privilage);
	}

	public void delatePrivilege(Privilege privilege) throws Exception {
		administrationDao.delatePrivilege(privilege);
	}

	public List<Privilege> getAllPrivileges() throws Exception {
		return administrationDao.getAllPrivileges();
	}

	// ----------------------------------------------------------//
	// --------------------Privilege-----------------------------//
	// ----------------------------------------------------------//
	public List<PrivilegeMapping> getAllPrivilegeMappingList()
			throws Exception {
		List<Privilege> privilegeList = administrationDao.getAllPrivileges();
		List<PrivilegeMapping> privilegeMappingList = new ArrayList<PrivilegeMapping>();
		for (Privilege privilege : privilegeList) {
			PrivilegeMapping privilegeMapping = new PrivilegeMapping();
			privilegeMapping.setPrivilegeId(privilege.getRecid());
			privilegeMapping.setPrivilegeName(privilege.getName());
			privilegeMappingList.add(privilegeMapping);
		}
		return privilegeMappingList;
	}

	public void saveAllPrivilegeMapping(
			List<PrivilegeMapping> PrivilegeMappingList, Long groupRecId)
			throws Exception {
		for (PrivilegeMapping privilegeMapping : PrivilegeMappingList) {
			if (privilegeMapping.getAccessId() == Constant.NO_ACCESS) {
				this.delateGroupPrivilages(new GroupPrivilege(groupRecId,
						privilegeMapping.getPrivilegeId()));
			}
			if (privilegeMapping.getAccessId() == Constant.ACCESS) {
				
//				Boolean b = isGroupPrivilagesFound(new GroupPrivilege(
//						groupRecId, privilegeMapping.getPrivilegeId(),
//						privilegeMapping.getAccessId()));
				Boolean b =(privilegeMapping.getRecId()==null?false:true);
				if (b == false) {
					this.addGroupPrivileges(new GroupPrivilege(groupRecId,
							privilegeMapping.getPrivilegeId(), privilegeMapping
									.getAccessId()));
				}
			}
		}
		setRegistredUserPrivileges(registeredUser);

	}

	// ----------------------------------------------------------//
	// -------------------GroupPrivilege---------------------//
	// ----------------------------------------------------------//

	public Long addGroupPrivileges(GroupPrivilege groupPrivilages)
			throws Exception {
		return administrationDao.addGroupPrivileges(groupPrivilages);
	}

	public void updateGroupPrivileges(GroupPrivilege groupPrivileges)
			throws Exception {
		administrationDao.updateGroupPrivileges(groupPrivileges);
	}

	public void delateGroupPrivilages(GroupPrivilege groupPrivileges)
			throws Exception {
		administrationDao.delateGroupPrivileges(groupPrivileges);

	}

//	public Boolean isGroupPrivilagesFound(GroupPrivilege groupPrivilege)
//			throws Exception {
//		return administrationDao.isGroupPrivilagesFound(groupPrivilege);
//	}

	public List<GroupPrivilege> getAllGroupPrivileges(Long groupRecId)
			throws Exception {
		return administrationDao.getAllGroupPrivileges(groupRecId);
	}

	public void saveAllGroupPrivilege(List<GroupPrivilege> groupPrivilegeList)
			throws Exception {
		for (GroupPrivilege groupPrivilege : groupPrivilegeList) {
			if (groupPrivilege.getGroupId() == null) {
				administrationDao.addGroupPrivileges(groupPrivilege);
			} else {
				administrationDao.updateGroupPrivileges(groupPrivilege);
			}
		}
	}

	// ----------------------------------------------------------//
	// --------------------------User----------------------------//
	// ----------------------------------------------------------//

	public User getUser(String userName, String password) throws Exception {
		return administrationDao.getUser(userName, password);
	}

	public Long addUser(User user) throws Exception {
		String pass = Utility.PasswordEncrypt(user.getPassword());
		user.setPassword(pass);
		return administrationDao.addUser(user);
	}

	public void updateUser(User user) throws Exception {
		String pass = Utility.PasswordEncrypt(user.getPassword());
		user.setPassword(pass);
		administrationDao.updateUser(user);
	}

	public void deleteUser(User user) throws Exception {
		administrationDao.deleteUser(user);

	} 

	public User getUserById(Long userId) throws Exception {
		User user = administrationDao.getUserById(userId);
		String pass = Utility.PasswordEncrypt(user.getPassword());
		user.setPassword(pass);
		return user;
	}

	public User getUser(String userName) throws Exception {
		return administrationDao.getUser(userName);
	}

	public User getUserByEmail(String email) throws Exception {
		return administrationDao.getUserByEmail(email);
	}

	public List<User> getListUserByGroupId(Long groupId) throws Exception {
		return administrationDao.getListUserByGroupId(groupId);
	}

	public List<User> getAllUsers() throws Exception {
		return administrationDao.getAllUsers();
	}

	public Groups getgroupById(Long groupId) throws Exception {
		return administrationDao.getGroupById(groupId);
	}

	public List<Groups> getListGroups() throws Exception {
		return administrationDao.getAllGroups();
	}

	// ----------------------------------------------------------//
	// ----------------------------------------------------------//
	// ----------------------------------------------------------//

	public Long saveGroupWithPrivileges(Groups group,
			List<PrivilegeMapping> PrivilegeMappingList) throws Exception {
		Long groupId;
		if (group.getRecid() == null) {
			groupId = administrationDao.addGroup(group);
		} else {
			administrationDao.updateGroup(group);
			groupId = group.getRecid();
		}

		// for (PrivilegeMapping privilegeMapping : PrivilegeMappingList) {
		//
		// if (privilegeMapping.getAccessId() == -1
		// || privilegeMapping.getAccessId() == Constant.NO_ACCESS) {
		// this.delateGroupPrivilages(new GroupPrivilege(group
		// .getGroupId(), privilegeMapping.getPrivilegeId()));
		// } else {
		// this.addGroupPrivileges(new GroupPrivilege(group.getGroupId(),
		// privilegeMapping.getPrivilegeId(), privilegeMapping
		// .getAccessId()));
		// }
		// }
		saveAllPrivilegeMapping(PrivilegeMappingList, groupId);
		return groupId;
	}

	public void deleteGroupWithPrivileges(Groups group) throws Exception {
		administrationDao.deleteAllGroupPrivilegesOfGroup(group.getRecid());
		administrationDao.deleteGroup(group);

	}

	public RegisteredUser loginUser(String userName, String password)
			throws Exception {

		User user = administrationDao.getUser(userName, password);
		if (user == null) {
			return null;
		}
		Groups group = administrationDao.getGroupById(user.getGroupId());
		if (group == null) {
			return null;
		}
		if (group.getStatus() != 1) {
			return null;
		}

		registeredUser.setUserId(user.getUserId());
		registeredUser.setGroupId(group.getRecid());
		setRegistredUserPrivileges(registeredUser);
		return registeredUser;

	}

	public void setRegistredUserPrivileges(RegisteredUser registeredUser)
			throws Exception {
		List<PrivilegeMapping> pml = administrationDao
				.getPrivilegeMappingList(registeredUser.getGroupId());
		registeredUser.setPrivilegeMappingList(pml);
		List<Long> categoryRecIdList = new ArrayList<Long>();
		boolean isCatIdFound = false;
		for (PrivilegeMapping pm : pml) {
			if (categoryRecIdList.size() == 0) {
				categoryRecIdList.add(pm.getCategoryId());
			} else {
				for (Long l : categoryRecIdList) {
					if (l.equals(pm.getCategoryId())) {
						isCatIdFound = true;
						break;
					} else {
						isCatIdFound = false;

					}
				}
				if (isCatIdFound == false) {
					categoryRecIdList.add(pm.getCategoryId());
				}
			}

		}
		registeredUser.setCategoryList(categoryRecIdList);
	}

	public List<PrivilegeMapping> getPrivilegeMappingList(Long groupId)
			throws Exception {
		return administrationDao.getPrivilegeMappingList(groupId);
	}

}
